<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@page import="sosr.serveur.IServeurRMI"%>
<%@page import="java.io.File"%>
<%@page import="sosr.bean.Medecin"%>
<%@page import="java.util.ArrayList"%>
<%@page import="sosr.bean.Patient"%>
<%@page import="bean.Valider"%>
<jsp:useBean id="pageConfig" scope="session" class="bean.Page"></jsp:useBean>
<jsp:useBean id="pageCGI" scope="session" class="bean.Page"></jsp:useBean>
<jsp:useBean id="logon" scope="session" class="bean.Connexion"></jsp:useBean>
<% 
int id = pageConfig.getAction_id();
System.out.println(id);

IServeurRMI serv = logon.getSrvRMI();
serv.open();

Medecin p = serv.selectMedecin(id);

String nom = "";
String prenom = "";
String adress = "";
String tel = "";
String ville = "";
String qualification = "";
String identifiant = "";
String num = "";
String mdp = "";
int cp = 0;

request.setCharacterEncoding("UTF-8");
if (p!= null) {
	 nom = p.getNom();
	 prenom = p.getPrenom();
	 adress = p.getAdresse();
	 tel = p.getTel();
	 ville = p.getVille();
	 qualification = p.getQualification();
	 identifiant = p.getIdent();
	 num = p.getNum();
	 cp = p.getCp();
}

if (request.getParameter("submit") != null) {
	//boolean res = Valider.form(request, patient);
	boolean res = true;
	
	id = Integer.parseInt(request.getParameter("id"));
	nom = request.getParameter("nom");
	prenom = request.getParameter("prenom");
	adress = request.getParameter("adresse");
	tel = request.getParameter("tel");
	ville = request.getParameter("ville");
	qualification = request.getParameter("qualification");
	cp = Integer.parseInt(request.getParameter("cp"));
	identifiant = request.getParameter("identifiant");
	num = request.getParameter("num");
	
	mdp = request.getParameter("mdp");
	
	if (res) {
		serv.open();
		Medecin medecin = new Medecin(id,identifiant,nom,prenom,adress,cp,ville,tel,qualification,num);
		
		//modifier le medecin
		boolean rp = serv.modMedecin(id,medecin);
		//modifier si besoin le mot de passe et identifiant
		boolean rp2 = true;
		if (mdp!="") rp2 = serv.modUtilisateur(id,identifiant,mdp);
		
		serv.close();
		
		if (rp && rp2) {
			pageCGI.setMessage("Notification","Vos informations personnelles ont &eacute;t&eacute; modifi&eacute;e avec succ&egrave;s !");
			pageConfig.setMedecin(medecin);
			response.sendRedirect("../../moncompte.jsp");
		}
		
	}
	else {
		pageCGI.setMessage("Erreur","Erreur lors de la modification de vos informations personnelles");
		response.sendRedirect("../../moncompte.jsp");
	}
}
else {

%>

<h1>Modifier mes informations</h1>

<form action="pages/moncompte/form_modinfoperso.jsp" method="post" id="forminfo_" >
<input type="hidden" name="id" value="<%=id %>" />
<input type="hidden" name="identifiant" id="identifiant" value="<%=identifiant %>" class="field" />
<table cellpadding="0" cellspacing="0" class="main_form">
	<tr>
		<td>Qualification&nbsp;</td>
		<td>
			<input type="text" name="qualification" id="qualification" value="<%=qualification %>" class="field" />
		</td>
	</tr>
	<tr>
		<td>Nom </td>
		<td>
			<input type="text" name="nom" id="nom" value="<%=nom %>" class="field" />
		</td>
	</tr>
	<tr>
		<td>Pr&eacute;nom </td>
		<td>
			<input type="text" name="prenom" id="prenom" value="<%=prenom %>" class="field" />
		</td>
	</tr>
	<tr>
		<td>SIRET </td>
		<td>
			<input type="text" name="num" id="num" value="<%=num %>" class="field" />
		</td>
	</tr>
	<tr>
		<td>T&eacute;l&eacute;phone </td>
		<td>
			<input type="text" name="tel" id="tel" value="<%=tel %>" class="field" />
		</td>
	</tr>
	<tr>
		<td>Adresse </td>
		<td>
			<input type="text" name="adresse" id="adresse" value="<%=adress %>" class="field" />
		</td>
	</tr>
	<tr>
		<td>Code postal </td>
		<td>
			<input type="text" name="cp" id="cp" value="<%=cp %>" class="field" />
		</td>
	</tr>
	<tr>
		<td>Ville </td>
		<td>
			<input type="text" name="ville" id="ville" value="<%=ville %>" class="field" />
		</td>
	</tr>
	<tr><td>&nbsp;</td><td></td></tr>
	<tr>
		<td>Identifiant </td>
		<td>
			<input type="text" name="identifiant" id="identifiant" value="<%=identifiant %>" class="field" />
		</td>
	</tr>
	<tr>
		<td>Mot de passe&nbsp;</td>
		<td>
			<input type="button" class="bouton" id="setmdp" onclick="$('#mdp').show('slow'); $('#resmdp').show(); $(this).hide();" value="Modifier" style="float:right; height: 22px; margin-top: 2px; padding-top: 2px;" />
			<input type="button" class="bouton" id="resmdp" onclick="$('#mdp').hide('slow').val(''); $('#setmdp').show(); $(this).hide();" value="Annuler" style="display: none; float:right; height: 22px; margin-top: 2px; padding-top: 2px;" />
			<input type="password" name="mdp" id="mdp" value="" class="field" style="display: none; float:left; margin-top: 5px; width:130px" />
		</td>
	</tr>
	<tr><td>&nbsp;</td><td></td></tr>
	<tr>
		<td colspan="2">
			<input type="submit" class="bouton" name="submit" value="Enregistrer" style="float: right"  />
			<input type="reset" class="bouton" value="R&eacute;initialiser" style="float: right" />
		</td>
	</tr>
</table>
</form>

<% } %>
<script type="text/javascript">
$("#forminfo_").submit(function() {
    if (check()) {
      return true;
    }
    alert2("Veuillez renseigner l'ensemble des champs");
    return false;
  });

function check() {
	if ($("#identifiant").val() == ""
		|| $("#nom").val() == ""
		|| $("#prenom").val() == ""
		|| $("#num").val() == ""
		|| $("#tel").val() == ""
		|| $("#adresse").val() == ""
		|| $("#cp").val() == ""
		|| $("#ville").val() == "") {
		return false;
	}
	else {
		return true;
	}
}
</script>
